package com.jye.rseven.user.controller;


import com.jye.rseven.domain.Address;
import com.jye.rseven.service.AddressService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Api(tags = "地址控制器API")
public class AddressController {
    @Autowired
    AddressService addressService;

    @GetMapping("/findAddressByUId")
    @ApiOperation(value = "查找我的地址", notes = "根据用户id查找我的地址", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findAddressByUId(int uId){
        JsonResult jsonResult = new JsonResult();
        List<Address> addressList = addressService.findAddressByUId(uId);
        if(addressList.size()!=0){
            jsonResult.setData(addressList);
        }else{
            jsonResult.setMsg("查询失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @PostMapping("/addAddress")
    @ApiOperation(value = "添加地址", notes = "通过地址address,名字name,电话phone添加我的地址", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "address",
                    value = "地址实体类", required = true,
                    dataType = "com.jye.rseven.domain.Address"),
    })
    public JsonResult addAddress(Address address){
        JsonResult jsonResult = new JsonResult();
        int i = addressService.addAddress(address);
        if(i==1){
            jsonResult.setMsg("添加成功!");
        }else{
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }

    @PutMapping("/updateAddress")
    @ApiOperation(value = "修改地址", notes = "修改地址", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "address",
                    value = "地址实体类", required = true,
                    dataType = "com.jye.rseven.domain.Address"),
    })
    public JsonResult updateAddress(Address address){
        JsonResult jsonResult = new JsonResult();
        int i = addressService.updateAddress(address);
        if(i==1){
            jsonResult.setMsg("添加成功!");
        }else{
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }


    @DeleteMapping("/deleteAddressById")
    @ApiOperation(value = "删除地址", notes = "删除地址", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult deleteAddress(int id){
        JsonResult jsonResult = new JsonResult();
        int i = addressService.deleteAddressById(id);
        if(i==1){
            jsonResult.setMsg("删除成功!");
        }else{
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }
}
